home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / Direct3D / BumpMapping / BumpEarth / BumpEarth.cpp next >
Encoding:
C/C++ Source or Header  |  2001-10-31  |  31.3 KB  |  852 lines

  1. //-----------------------------------------------------------------------------
  2. // File: BumpEarth.cpp
  3. //
  4. // Desc: Direct3D environment mapping / bump mapping sample. The technique
  5. //       used perturbs the environment map to simulate bump mapping.
  6. //
  7. //
  8. // Copyright (c) 1997-2001 Microsoft Corporation. All rights reserved.
  9. //-----------------------------------------------------------------------------
  10. #define STRICT
  11. #include <math.h>
  12. #include <stdio.h>
  13. #include <D3DX8.h>
  14. #include "D3DApp.h"
  15. #include "D3DFont.h"
  16. #include "D3DUtil.h"
  17. #include "DXUtil.h"
  18. #include "resource.h"
  19.  
  20.  
  21.  
  22.  
  23. //-----------------------------------------------------------------------------
  24. // Defines, constants, and global variables
  25. //-----------------------------------------------------------------------------
  26.  
  27. // Vertex with 2nd set of tex coords (for bumpmapped environment map)
  28. struct BUMPVERTEX
  29. {
  30.     D3DXVECTOR3 p;
  31.     D3DXVECTOR3 n;
  32.     FLOAT       tu1, tv1;
  33.     FLOAT       tu2, tv2;
  34. };
  35.  
  36. #define D3DFVF_BUMPVERTEX (D3DFVF_XYZ|D3DFVF_NORMAL|D3DFVF_TEX2)
  37.  
  38. // Converts a FLOAT to a DWORD for use in SetRenderState() calls
  39. inline DWORD F2DW( FLOAT f ) { return *((DWORD*)&f); }
  40.  
  41.  
  42.  
  43.  
  44. //-----------------------------------------------------------------------------
  45. // Name: class CMyD3DApplication
  46. // Desc: Application class. The base class (CD3DApplication) provides the 
  47. //       generic functionality needed in all Direct3D samples. CMyD3DApplication 
  48. //       adds functionality specific to this sample program.
  49. //-----------------------------------------------------------------------------
  50. class CMyD3DApplication : public CD3DApplication
  51. {
  52.     CD3DFont*     m_pFont;                  // A font to output text
  53.  
  54.     CD3DArcBall   m_ArcBall;                // ArcBall used for mouse input
  55.  
  56.     LPDIRECT3DTEXTURE8 m_pBlockTexture;     // A blank, gray texture
  57.     LPDIRECT3DTEXTURE8 m_pEarthTexture;     // The Earth texture
  58.     LPDIRECT3DTEXTURE8 m_pEnvMapTexture;    // The environment map
  59.     LPDIRECT3DTEXTURE8 m_pEarthBumpTexture; // Source for the bumpmap
  60.     LPDIRECT3DTEXTURE8 m_psBumpMap;         // The actual bumpmap
  61.  
  62.     D3DFORMAT     m_BumpMapFormat;         // Bumpmap texture format
  63.     LPDIRECT3DVERTEXBUFFER8 m_pEarthVB;    // Geometry for the Earth
  64.     DWORD         m_dwNumSphereVertices;
  65.  
  66.     BOOL          m_bHighTesselation;      // User options
  67.     BOOL          m_bTextureOn;
  68.     BOOL          m_bBumpMapOn;
  69.     BOOL          m_bEnvMapOn;
  70.     BOOL          m_bDeviceValidationFailed;
  71.  
  72.     // Internal functions
  73.     VOID    SetMenuStates();
  74.     HRESULT CreateEarthVertexBuffer();
  75.     VOID    ApplyEnvironmentMap();
  76.     HRESULT InitBumpMap();
  77.  
  78. protected:
  79.     HRESULT OneTimeSceneInit();
  80.     HRESULT InitDeviceObjects();
  81.     HRESULT RestoreDeviceObjects();
  82.     HRESULT InvalidateDeviceObjects();
  83.     HRESULT DeleteDeviceObjects();
  84.     HRESULT Render();
  85.     HRESULT FrameMove();
  86.     HRESULT FinalCleanup();
  87.     HRESULT ConfirmDevice( D3DCAPS8*, DWORD, D3DFORMAT );
  88.  
  89. public:
  90.     CMyD3DApplication();
  91.  
  92.     LRESULT MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
  93. };
  94.  
  95.  
  96.  
  97.  
  98. //-----------------------------------------------------------------------------
  99. // Name: WinMain()
  100. // Desc: Entry point to the program. Initializes everything, and goes into a
  101. //       message-processing loop. Idle time is used to render the scene.
  102. //-----------------------------------------------------------------------------
  103. INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
  104. {
  105.     CMyD3DApplication d3dApp;
  106.  
  107.     if( FAILED( d3dApp.Create( hInst ) ) )
  108.         return 0;
  109.  
  110.     return d3dApp.Run();
  111. }
  112.  
  113.  
  114.  
  115.  
  116. //-----------------------------------------------------------------------------
  117. // Name: CMyD3DApplication()
  118. // Desc: Application constructor. Sets attributes for the app.
  119. //-----------------------------------------------------------------------------
  120. CMyD3DApplication::CMyD3DApplication()
  121. {
  122.     m_strWindowTitle    = _T("BumpEarth: Direct3D BumpMapping Demo");
  123.     m_bUseDepthBuffer   = TRUE;
  124.     m_bShowCursorWhenFullscreen = TRUE;
  125.  
  126.     m_psBumpMap         = NULL;
  127.     m_bTextureOn        = TRUE;
  128.     m_bBumpMapOn        = TRUE;
  129.     m_bEnvMapOn         = TRUE;
  130.     m_bHighTesselation  = TRUE;
  131.  
  132.     m_pBlockTexture     = NULL;
  133.     m_pEarthTexture     = NULL;
  134.     m_pEarthBumpTexture = NULL;
  135.     m_pEnvMapTexture    = NULL;
  136.     m_bDeviceValidationFailed = FALSE;
  137.  
  138.     m_pFont             = new CD3DFont( _T("Arial"), 12, D3DFONT_BOLD );
  139.     m_pEarthVB          = NULL;
  140. }
  141.  
  142.  
  143.  
  144.  
  145. //-----------------------------------------------------------------------------
  146. // Name: OneTimeSceneInit()
  147. // Desc: Called during initial app startup, this function performs all the
  148. //       permanent initialization.
  149. //-----------------------------------------------------------------------------
  150. HRESULT CMyD3DApplication::OneTimeSceneInit()
  151. {
  152.     // Set cursor to indicate that user can move the object with the mouse
  153. #ifdef _WIN64
  154.     SetClassLongPtr( m_hWnd, GCLP_HCURSOR, (LONG_PTR)LoadCursor( NULL, IDC_SIZEALL ) );
  155. #else
  156.     SetClassLong( m_hWnd, GCL_HCURSOR, (LONG)LoadCursor( NULL, IDC_SIZEALL ) );
  157. #endif
  158.     return S_OK;
  159. }
  160.  
  161.  
  162.  
  163.  
  164. //-----------------------------------------------------------------------------
  165. // Name: ApplyEnvironmentMap()
  166. // Desc: Performs a calculation on each of the vertices' normals to determine
  167. //       what the texture coordinates should be for the environment map.
  168. //-----------------------------------------------------------------------------
  169. VOID CMyD3DApplication::ApplyEnvironmentMap()
  170. {
  171.     // Get the World-View(WV) matrix set
  172.     D3DXMATRIX matWorld, matView, matWorldView;
  173.     m_pd3dDevice->GetTransform( D3DTS_WORLD, &matWorld );
  174.     m_pd3dDevice->GetTransform( D3DTS_VIEW,  &matView );
  175.     D3DXMatrixMultiply( &matWorldView, &matWorld, &matView );
  176.  
  177.     // Lock the vertex buffer
  178.     BUMPVERTEX* vtx;
  179.     m_pEarthVB->Lock( 0, 0, (BYTE**)&vtx, 0 );
  180.  
  181.     // Establish constants used in sphere generation
  182.     DWORD dwNumSphereRings    = m_bHighTesselation ? 15 :  5;
  183.     DWORD dwNumSphereSegments = m_bHighTesselation ? 30 : 10;
  184.     FLOAT fDeltaRingAngle = ( D3DX_PI / dwNumSphereRings );
  185.     FLOAT fDeltaSegAngle  = ( 2.0f * D3DX_PI / dwNumSphereSegments );
  186.  
  187.     D3DXVECTOR4 vT;
  188.     FLOAT fScale;
  189.  
  190.     // Generate the group of rings for the sphere
  191.     for( DWORD ring = 0; ring < dwNumSphereRings; ring++ )
  192.     {
  193.         FLOAT r0 = sinf( (ring+0) * fDeltaRingAngle );
  194.         FLOAT r1 = sinf( (ring+1) * fDeltaRingAngle );
  195.         FLOAT y0 = cosf( (ring+0) * fDeltaRingAngle );
  196.         FLOAT y1 = cosf( (ring+1) * fDeltaRingAngle );
  197.  
  198.         // Generate the group of segments for the current ring
  199.         for( DWORD seg = 0; seg < (dwNumSphereSegments+1); seg++ )
  200.         {
  201.             FLOAT x0 =  r0 * sinf( seg * fDeltaSegAngle );
  202.             FLOAT z0 =  r0 * cosf( seg * fDeltaSegAngle );
  203.             FLOAT x1 =  r1 * sinf( seg * fDeltaSegAngle );
  204.             FLOAT z1 =  r1 * cosf( seg * fDeltaSegAngle );
  205.  
  206.             // Add two vertices to the strip which makes up the sphere
  207.             // (using the transformed normal to generate texture coords)
  208.             (*vtx).p   = (*vtx).n   = D3DXVECTOR3(x0,y0,z0);
  209.             (*vtx).tu1 = (*vtx).tu2 = -((FLOAT)seg)/dwNumSphereSegments;
  210.             (*vtx).tv1 = (*vtx).tv2 = (ring+0)/(FLOAT)dwNumSphereRings;
  211.             D3DXVec3Transform( &vT, &(*vtx).n, &matWorldView );
  212.             fScale = 1.37f / D3DXVec4Length( &vT );
  213.             (*vtx).tu1 = 0.5f + fScale*vT.x;
  214.             (*vtx).tv1 = 0.5f - fScale*vT.y;
  215.             vtx++;
  216.  
  217.             (*vtx).p   = (*vtx).n   = D3DXVECTOR3(x1,y1,z1);
  218.             (*vtx).tu1 = (*vtx).tu2 = -((FLOAT)seg)/dwNumSphereSegments;
  219.             (*vtx).tv1 = (*vtx).tv2 = (ring+1)/(FLOAT)dwNumSphereRings;
  220.             D3DXVec3Transform( &vT, &(*vtx).n, &matWorldView );
  221.             fScale = 1.37f / D3DXVec4Length( &vT );
  222.             (*vtx).tu1 = 0.5f + fScale*vT.x;
  223.             (*vtx).tv1 = 0.5f - fScale*vT.y;
  224.             vtx++;
  225.         }
  226.     }
  227.  
  228.     m_pEarthVB->Unlock();
  229. }
  230.  
  231.  
  232.  
  233.  
  234. //-----------------------------------------------------------------------------
  235. // Name: FrameMove()
  236. // Desc: Animates the scene
  237. //-----------------------------------------------------------------------------
  238. HRESULT CMyD3DApplication::FrameMove()
  239. {
  240.     // Update the Earth's rotation angle
  241.     static FLOAT fRotationAngle = 0.0f;
  242.     if( FALSE == m_ArcBall.IsBeingDragged() )
  243.         fRotationAngle += m_fElapsedTime;
  244.  
  245.     // Setup viewing postion from ArcBall
  246.     D3DXMATRIX matWorld;
  247.     D3DXMatrixRotationY( &matWorld, -fRotationAngle );
  248.     D3DXMatrixMultiply( &matWorld, &matWorld, m_ArcBall.GetRotationMatrix() );
  249.     D3DXMatrixMultiply( &matWorld, &matWorld, m_ArcBall.GetTranslationMatrix() );
  250.     m_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
  251.  
  252.     D3DXMATRIX  matView;
  253.     D3DXVECTOR3 vEyePt    = D3DXVECTOR3( 0.0f, 0.0f, -3.0f );
  254.     D3DXVECTOR3 vLookatPt = D3DXVECTOR3( 0.0f, 0.0f, 0.0f );
  255.     D3DXVECTOR3 vUpVec    = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
  256.     D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt, &vUpVec );
  257.     m_pd3dDevice->SetTransform( D3DTS_VIEW, &matView );
  258.  
  259.     // Apply the environment map
  260.     ApplyEnvironmentMap();
  261.  
  262.     return S_OK;
  263. }
  264.  
  265.  
  266.  
  267.  
  268. //-----------------------------------------------------------------------------
  269. // Name: Render()
  270. // Desc: Renders the scene.
  271. //-----------------------------------------------------------------------------
  272. HRESULT CMyD3DApplication::Render()
  273. {
  274.     DWORD dwNumPasses;
  275.  
  276.     m_pd3dDevice->Clear( 0L, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,
  277.                          0x00000000, 1.0f, 0L );
  278.  
  279.     if( FAILED( m_pd3dDevice->BeginScene() ) )
  280.         return S_OK; // Don't return a "fatal" error
  281.  
  282.     m_pd3dDevice->SetRenderState( D3DRS_WRAP0, D3DWRAP_U | D3DWRAP_V );
  283.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE );
  284.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
  285.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
  286.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1 );
  287.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
  288.     m_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLOROP, D3DTOP_DISABLE );
  289.  
  290.     if( m_bTextureOn )
  291.         m_pd3dDevice->SetTexture( 0, m_pEarthTexture );
  292.     else
  293.         m_pd3dDevice->SetTexture( 0, m_pBlockTexture );
  294.  
  295.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_TEXCOORDINDEX, 1 );
  296.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
  297.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
  298.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
  299.  
  300.     if( m_bBumpMapOn && m_bEnvMapOn )
  301.     {
  302.         m_pd3dDevice->SetTexture( 1, m_psBumpMap );
  303.         m_pd3dDevice->SetTextureStageState( 1, D3DTSS_TEXCOORDINDEX, 1 );
  304.         if( m_BumpMapFormat == D3DFMT_V8U8 )
  305.             m_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLOROP, D3DTOP_BUMPENVMAP );
  306.         else
  307.             m_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLOROP, D3DTOP_BUMPENVMAPLUMINANCE );
  308.         m_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLORARG1, D3DTA_TEXTURE );
  309.         m_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLORARG2, D3DTA_CURRENT );
  310.         m_pd3dDevice->SetTextureStageState( 1, D3DTSS_BUMPENVMAT00, F2DW(0.5f) );
  311.         m_pd3dDevice->SetTextureStageState( 1, D3DTSS_BUMPENVMAT01, F2DW(0.0f) );
  312.         m_pd3dDevice->SetTextureStageState( 1, D3DTSS_BUMPENVMAT10, F2DW(0.0f) );
  313.         m_pd3dDevice->SetTextureStageState( 1, D3DTSS_BUMPENVMAT11, F2DW(0.5f) );
  314.         m_pd3dDevice->SetTextureStageState( 1, D3DTSS_BUMPENVLSCALE, F2DW(4.0f) );
  315.         m_pd3dDevice->SetTextureStageState( 1, D3DTSS_BUMPENVLOFFSET, F2DW(0.0f) );
  316.  
  317.         if( m_bEnvMapOn )
  318.         {
  319.             m_pd3dDevice->SetTexture( 2, m_pEnvMapTexture );
  320.             m_pd3dDevice->SetTextureStageState( 2, D3DTSS_TEXCOORDINDEX, 0 );
  321.             m_pd3dDevice->SetTextureStageState( 2, D3DTSS_COLOROP, D3DTOP_ADD );
  322.             m_pd3dDevice->SetTextureStageState( 2, D3DTSS_COLORARG1, D3DTA_TEXTURE );
  323.             m_pd3dDevice->SetTextureStageState( 2, D3DTSS_COLORARG2, D3DTA_CURRENT );
  324.         }
  325.         else
  326.             m_pd3dDevice->SetTextureStageState( 2, D3DTSS_COLOROP, D3DTOP_DISABLE );
  327.     }
  328.     else
  329.     {
  330.         if( m_bEnvMapOn )
  331.         {
  332.             m_pd3dDevice->SetTexture( 1, m_pEnvMapTexture );
  333.             m_pd3dDevice->SetTextureStageState( 1, D3DTSS_TEXCOORDINDEX, 0 );
  334.             m_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLOROP, D3DTOP_ADD );
  335.             m_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLORARG1, D3DTA_TEXTURE );
  336.             m_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLORARG2, D3DTA_CURRENT );
  337.         }
  338.         else
  339.             m_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLOROP, D3DTOP_DISABLE );
  340.  
  341.         m_pd3dDevice->SetTextureStageState( 2, D3DTSS_COLOROP, D3DTOP_DISABLE );
  342.     }
  343.  
  344.     m_pd3dDevice->SetStreamSource( 0, m_pEarthVB, sizeof(BUMPVERTEX) );
  345.     m_pd3dDevice->SetVertexShader( D3DFVF_BUMPVERTEX );
  346.  
  347.     if( FAILED( m_pd3dDevice->ValidateDevice( &dwNumPasses ) ) )
  348.     {
  349.         // The right thing to do when device validation fails is to try
  350.         // a different rendering technique.  This sample just warns the user.
  351.         m_bDeviceValidationFailed = TRUE;
  352.     }
  353.     else
  354.     {
  355.         m_bDeviceValidationFailed = FALSE;
  356.     }
  357.  
  358.     // Finally, draw the Earth
  359.     m_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, m_dwNumSphereVertices-2 );
  360.  
  361.     // Restore texture stage states
  362.     m_pd3dDevice->SetTexture( 0, NULL );
  363.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_TEXCOORDINDEX, 0 );
  364.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
  365.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
  366.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
  367.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_DISABLE );
  368.     m_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLOROP, D3DTOP_DISABLE );
  369.     m_pd3dDevice->SetTextureStageState( 1, D3DTSS_ALPHAOP, D3DTOP_DISABLE );
  370.     m_pd3dDevice->SetTextureStageState( 2, D3DTSS_COLOROP, D3DTOP_DISABLE );
  371.     m_pd3dDevice->SetTextureStageState( 2, D3DTSS_ALPHAOP, D3DTOP_DISABLE );
  372.  
  373.     // Output statistics
  374.     m_pFont->DrawText( 2,  0, D3DCOLOR_ARGB(255,255,255,0), m_strFrameStats );
  375.     m_pFont->DrawText( 2, 20, D3DCOLOR_ARGB(255,255,255,0), m_strDeviceStats );
  376.  
  377.     if( m_bDeviceValidationFailed )
  378.     {
  379.         m_pFont->DrawText( 2, 40, D3DCOLOR_ARGB(255,255,0,0), 
  380.             _T("Warning: Device validation failed.  Rendering may not look right.") );
  381.     }
  382.  
  383.     m_pd3dDevice->EndScene();
  384.  
  385.     return S_OK;
  386. }
  387.  
  388.  
  389.  
  390.  
  391. //-----------------------------------------------------------------------------
  392. // Name: InitBumpMap()
  393. // Desc: Converts data from m_pEarthBumpTexture into the type of bump map requested
  394. //       as m_BumpMapFormat into m_psBumpMap.
  395. //-----------------------------------------------------------------------------
  396. HRESULT CMyD3DApplication::InitBumpMap()
  397. {
  398.     LPDIRECT3DTEXTURE8 psBumpSrc = m_pEarthBumpTexture;
  399.     D3DSURFACE_DESC    d3dsd;
  400.     D3DLOCKED_RECT     d3dlr;
  401.  
  402.     psBumpSrc->GetLevelDesc( 0, &d3dsd );
  403.     // Create the bumpmap's surface and texture objects
  404.     if( FAILED( m_pd3dDevice->CreateTexture( d3dsd.Width, d3dsd.Height, 1, 0, 
  405.         m_BumpMapFormat, D3DPOOL_MANAGED, &m_psBumpMap ) ) )
  406.     {
  407.         return E_FAIL;
  408.     }
  409.  
  410.     // Fill the bits of the new texture surface with bits from
  411.     // a private format.
  412.     psBumpSrc->LockRect( 0, &d3dlr, 0, 0 );
  413.     DWORD dwSrcPitch = (DWORD)d3dlr.Pitch;
  414.     BYTE* pSrcTopRow = (BYTE*)d3dlr.pBits;
  415.     BYTE* pSrcCurRow = pSrcTopRow;
  416.     BYTE* pSrcBotRow = pSrcTopRow + (dwSrcPitch * (d3dsd.Height - 1) );
  417.  
  418.     m_psBumpMap->LockRect( 0, &d3dlr, 0, 0 );
  419.     DWORD dwDstPitch = (DWORD)d3dlr.Pitch;
  420.     BYTE* pDstTopRow = (BYTE*)d3dlr.pBits;
  421.     BYTE* pDstCurRow = pDstTopRow;
  422.     BYTE* pDstBotRow = pDstTopRow + (dwDstPitch * (d3dsd.Height - 1) );
  423.  
  424.     for( DWORD y=0; y<d3dsd.Height; y++ )
  425.     {
  426.         BYTE* pSrcB0; // addr of current pixel
  427.         BYTE* pSrcB1; // addr of pixel below current pixel, wrapping to top if necessary
  428.         BYTE* pSrcB2; // addr of pixel above current pixel, wrapping to bottom if necessary
  429.         BYTE* pDstT;  // addr of dest pixel;
  430.  
  431.         pSrcB0 = pSrcCurRow;
  432.  
  433.         if( y == d3dsd.Height - 1)
  434.             pSrcB1 = pSrcTopRow;
  435.         else
  436.             pSrcB1 = pSrcCurRow + dwSrcPitch;
  437.  
  438.         if( y == 0 )
  439.             pSrcB2 = pSrcBotRow;
  440.         else
  441.             pSrcB2 = pSrcCurRow - dwSrcPitch;
  442.  
  443.         pDstT = pDstCurRow;
  444.  
  445.         for( DWORD x=0; x<d3dsd.Width; x++ )
  446.         {
  447.             LONG v00; // Current pixel
  448.             LONG v01; // Pixel to the right of current pixel, wrapping to left edge if necessary
  449.             LONG vM1; // Pixel to the left of current pixel, wrapping to right edge if necessary
  450.             LONG v10; // Pixel one line below.
  451.             LONG v1M; // Pixel one line above.
  452.  
  453.             v00 = *(pSrcB0+0);
  454.             
  455.             if( x == d3dsd.Width - 1 )
  456.                 v01 = *(pSrcCurRow);
  457.             else
  458.                 v01 = *(pSrcB0+4);
  459.             
  460.             if( x == 0 )
  461.                 vM1 = *(pSrcCurRow + (4 * (d3dsd.Width - 1)));
  462.             else
  463.                 vM1 = *(pSrcB0-4);
  464.             v10 = *(pSrcB1+0);
  465.             v1M = *(pSrcB2+0);
  466.  
  467.             LONG iDu = (vM1-v01); // The delta-u bump value
  468.             LONG iDv = (v1M-v10); // The delta-v bump value
  469.  
  470.             // The luminance bump value (land masses are less shiny)
  471.             WORD uL = ( v00>1 ) ? 63 : 127;
  472.  
  473.             switch( m_BumpMapFormat )
  474.             {
  475.                 case D3DFMT_V8U8:
  476.                     *pDstT++ = (BYTE)iDu;
  477.                     *pDstT++ = (BYTE)iDv;
  478.                     break;
  479.  
  480.                 case D3DFMT_L6V5U5:
  481.                     *(WORD*)pDstT  = (WORD)( ( (iDu>>3) & 0x1f ) <<  0 );
  482.                     *(WORD*)pDstT |= (WORD)( ( (iDv>>3) & 0x1f ) <<  5 );
  483.                     *(WORD*)pDstT |= (WORD)( ( ( uL>>2) & 0x3f ) << 10 );
  484.                     pDstT += 2;
  485.                     break;
  486.  
  487.                 case D3DFMT_X8L8V8U8:
  488.                     *pDstT++ = (BYTE)iDu;
  489.                     *pDstT++ = (BYTE)iDv;
  490.                     *pDstT++ = (BYTE)uL;
  491.                     *pDstT++ = (BYTE)0L;
  492.                     break;
  493.             }
  494.  
  495.             // Move one pixel to the right (src is 32-bpp)
  496.             pSrcB0+=4;
  497.             pSrcB1+=4;
  498.             pSrcB2+=4;
  499.         }
  500.  
  501.         // Move to the next line
  502.         pSrcCurRow += dwSrcPitch;
  503.         pDstCurRow += dwDstPitch;
  504.     }
  505.  
  506.     m_psBumpMap->UnlockRect(0);
  507.     psBumpSrc->UnlockRect(0);
  508.  
  509.     return S_OK;
  510. }
  511.  
  512.  
  513.  
  514.  
  515. //-----------------------------------------------------------------------------
  516. // Name: InitDeviceObjects()
  517. // Desc: Initialize scene objects.
  518. //-----------------------------------------------------------------------------
  519. HRESULT CMyD3DApplication::InitDeviceObjects()
  520. {
  521.     m_pFont->InitDeviceObjects( m_pd3dDevice );
  522.  
  523.     if( FAILED( D3DUtil_CreateTexture( m_pd3dDevice, _T("Block.bmp"),
  524.                                        &m_pBlockTexture, D3DFMT_R5G6B5 ) ) )
  525.         return D3DAPPERR_MEDIANOTFOUND;
  526.     if( FAILED( D3DUtil_CreateTexture( m_pd3dDevice, _T("Earth.bmp"),
  527.                                        &m_pEarthTexture, D3DFMT_R5G6B5 ) ) )
  528.         return D3DAPPERR_MEDIANOTFOUND;
  529.     if( FAILED( D3DUtil_CreateTexture( m_pd3dDevice, _T("Earthbump.bmp"),
  530.                                        &m_pEarthBumpTexture, D3DFMT_A8R8G8B8 ) ) )
  531.         return D3DAPPERR_MEDIANOTFOUND;
  532.     if( FAILED( D3DUtil_CreateTexture( m_pd3dDevice, _T("EarthEnvMap.bmp"),
  533.                                        &m_pEnvMapTexture, D3DFMT_R5G6B5 ) ) )
  534.         return D3DAPPERR_MEDIANOTFOUND;
  535.  
  536.     // Find out which bump map texture are supported by this device
  537.     BOOL bCanDoV8U8   = SUCCEEDED( m_pD3D->CheckDeviceFormat( m_d3dCaps.AdapterOrdinal,
  538.                                    m_d3dCaps.DeviceType, m_d3dsdBackBuffer.Format,
  539.                                    0, D3DRTYPE_TEXTURE,
  540.                                    D3DFMT_V8U8 ) ) &&
  541.                         (m_d3dCaps.TextureOpCaps & D3DTEXOPCAPS_BUMPENVMAP );
  542.  
  543.     BOOL bCanDoL6V5U5 = SUCCEEDED( m_pD3D->CheckDeviceFormat( m_d3dCaps.AdapterOrdinal,
  544.                                    m_d3dCaps.DeviceType, m_d3dsdBackBuffer.Format,
  545.                                    0, D3DRTYPE_TEXTURE,
  546.                                    D3DFMT_L6V5U5 ) ) &&
  547.                         (m_d3dCaps.TextureOpCaps & D3DTEXOPCAPS_BUMPENVMAPLUMINANCE );
  548.  
  549.     BOOL bCanDoL8V8U8 = SUCCEEDED( m_pD3D->CheckDeviceFormat( m_d3dCaps.AdapterOrdinal,
  550.                                    m_d3dCaps.DeviceType, m_d3dsdBackBuffer.Format,
  551.                                    0, D3DRTYPE_TEXTURE,
  552.                                    D3DFMT_X8L8V8U8 ) ) &&
  553.                         (m_d3dCaps.TextureOpCaps & D3DTEXOPCAPS_BUMPENVMAPLUMINANCE );
  554.  
  555.     if( bCanDoV8U8 )        m_BumpMapFormat = D3DFMT_V8U8;
  556.     else if( bCanDoL6V5U5 ) m_BumpMapFormat = D3DFMT_L6V5U5;
  557.     else if( bCanDoL8V8U8 ) m_BumpMapFormat = D3DFMT_X8L8V8U8;
  558.     else                    return E_FAIL;
  559.  
  560.     // Set menu states
  561.     HMENU hMenu = GetMenu( m_hWnd );
  562.     EnableMenuItem( hMenu, IDM_U8V8,   bCanDoV8U8   ? MF_ENABLED : MF_GRAYED );
  563.     EnableMenuItem( hMenu, IDM_U5V5L6, bCanDoL6V5U5 ? MF_ENABLED : MF_GRAYED );
  564.     EnableMenuItem( hMenu, IDM_U8V8L8, bCanDoL8V8U8 ? MF_ENABLED : MF_GRAYED );
  565.     SetMenuStates();
  566.  
  567.     // Initialize earth geometry
  568.     if( FAILED( CreateEarthVertexBuffer() ) )
  569.         return E_FAIL;
  570.  
  571.     // Create and fill the bumpmap
  572.     if( FAILED( InitBumpMap() ) )
  573.         return E_FAIL;
  574.  
  575.     m_bDeviceValidationFailed = FALSE;
  576.  
  577.     return S_OK;
  578. }
  579.  
  580.  
  581.  
  582.  
  583. //-----------------------------------------------------------------------------
  584. // Name: RestoreDeviceObjects()
  585. // Desc: Initialize scene objects.
  586. //-----------------------------------------------------------------------------
  587. HRESULT CMyD3DApplication::RestoreDeviceObjects()
  588. {
  589.     m_pFont->RestoreDeviceObjects();
  590.  
  591.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ADDRESSU, D3DTADDRESS_WRAP );
  592.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ADDRESSV, D3DTADDRESS_WRAP );
  593.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR );
  594.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MINFILTER, D3DTEXF_LINEAR );
  595.     m_pd3dDevice->SetTextureStageState( 1, D3DTSS_ADDRESSU, D3DTADDRESS_WRAP );
  596.     m_pd3dDevice->SetTextureStageState( 1, D3DTSS_ADDRESSV, D3DTADDRESS_WRAP );
  597.     m_pd3dDevice->SetTextureStageState( 1, D3DTSS_MAGFILTER, D3DTEXF_LINEAR );
  598.     m_pd3dDevice->SetTextureStageState( 1, D3DTSS_MINFILTER, D3DTEXF_LINEAR );
  599.     m_pd3dDevice->SetTextureStageState( 2, D3DTSS_ADDRESSU, D3DTADDRESS_WRAP );
  600.     m_pd3dDevice->SetTextureStageState( 2, D3DTSS_ADDRESSV, D3DTADDRESS_WRAP );
  601.     m_pd3dDevice->SetTextureStageState( 2, D3DTSS_MAGFILTER, D3DTEXF_LINEAR );
  602.     m_pd3dDevice->SetTextureStageState( 2, D3DTSS_MINFILTER, D3DTEXF_LINEAR );
  603.  
  604.     m_pd3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE );
  605.  
  606.     // Get the aspect ratio
  607.     FLOAT fAspect = ((FLOAT)m_d3dsdBackBuffer.Width) / m_d3dsdBackBuffer.Height;
  608.  
  609.     // Set projection matrix
  610.     D3DXMATRIX matProj;
  611.     D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, fAspect, 0.1f, 25.0f );
  612.     m_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
  613.  
  614.     // Set the ArcBall parameters
  615.     m_ArcBall.SetWindow( m_d3dsdBackBuffer.Width, m_d3dsdBackBuffer.Height, 3.0f );
  616.     m_ArcBall.SetRadius( 1.0f );
  617.  
  618.     return S_OK;
  619. }
  620.  
  621.  
  622.  
  623.  
  624. //-----------------------------------------------------------------------------
  625. // Name: InvalidateDeviceObjects()
  626. // Desc:
  627. //-----------------------------------------------------------------------------
  628. HRESULT CMyD3DApplication::InvalidateDeviceObjects()
  629. {
  630.     m_pFont->InvalidateDeviceObjects();
  631.     return S_OK;
  632. }
  633.  
  634.  
  635.  
  636.  
  637. //-----------------------------------------------------------------------------
  638. // Name: DeleteDeviceObjects()
  639. // Desc: Called when the app is exiting, or the device is being changed,
  640. //       this function deletes any device dependent objects.
  641. //-----------------------------------------------------------------------------
  642. HRESULT CMyD3DApplication::DeleteDeviceObjects()
  643. {
  644.     SAFE_RELEASE( m_pBlockTexture );
  645.     SAFE_RELEASE( m_pEarthTexture );
  646.     SAFE_RELEASE( m_pEarthBumpTexture );
  647.     SAFE_RELEASE( m_pEnvMapTexture );
  648.  
  649.     m_pFont->DeleteDeviceObjects();
  650.  
  651.     SAFE_RELEASE( m_psBumpMap );
  652.     SAFE_RELEASE( m_pEarthVB );
  653.     return S_OK;
  654. }
  655.  
  656.  
  657.  
  658.  
  659. //-----------------------------------------------------------------------------
  660. // Name: FinalCleanup()
  661. // Desc: Called before the app exits, this function gives the app the chance
  662. //       to cleanup after itself.
  663. //-----------------------------------------------------------------------------
  664. HRESULT CMyD3DApplication::FinalCleanup()
  665. {
  666.     SAFE_DELETE( m_pFont );
  667.  
  668.     return S_OK;
  669. }
  670.  
  671.  
  672.  
  673.  
  674. //-----------------------------------------------------------------------------
  675. // Name: ConfirmDevice()
  676. // Desc: Called during device intialization, this code checks the device
  677. //       for some minimum set of capabilities
  678. //-----------------------------------------------------------------------------
  679. HRESULT CMyD3DApplication::ConfirmDevice( D3DCAPS8* pCaps, DWORD dwBehavior,
  680.                                           D3DFORMAT Format )
  681. {
  682.     if( dwBehavior & D3DCREATE_PUREDEVICE )
  683.         return E_FAIL; // GetTransform doesn't work on PUREDEVICE
  684.  
  685.     // Device must be able to do bumpmapping
  686.     if( pCaps->TextureOpCaps & D3DTEXOPCAPS_BUMPENVMAPLUMINANCE )
  687.     {
  688.         // Accept devices that can create D3DFMT_X8L8V8U8 textures
  689.         if( SUCCEEDED( m_pD3D->CheckDeviceFormat( pCaps->AdapterOrdinal,
  690.                                                   pCaps->DeviceType, Format,
  691.                                                   0, D3DRTYPE_TEXTURE,
  692.                                                   D3DFMT_X8L8V8U8 ) ) )
  693.         {
  694.             return S_OK;
  695.         }
  696.  
  697.         // Accept devices that can create D3DFMT_L6V5U5 textures
  698.         if( SUCCEEDED( m_pD3D->CheckDeviceFormat( pCaps->AdapterOrdinal,
  699.                                                   pCaps->DeviceType, Format,
  700.                                                   0, D3DRTYPE_TEXTURE,
  701.                                                   D3DFMT_L6V5U5 ) ) )
  702.         {
  703.             return S_OK;
  704.         }
  705.     }
  706.  
  707.     if( pCaps->TextureOpCaps & D3DTEXOPCAPS_BUMPENVMAP )
  708.     {
  709.         // Accept devices that can create D3DFMT_V8U8 textures
  710.         if( SUCCEEDED( m_pD3D->CheckDeviceFormat( pCaps->AdapterOrdinal,
  711.                                                   pCaps->DeviceType, Format,
  712.                                                   0, D3DRTYPE_TEXTURE,
  713.                                                   D3DFMT_V8U8 ) ) )
  714.         {
  715.             return S_OK;
  716.         }
  717.     }
  718.  
  719.     // Else, reject the device
  720.     return E_FAIL;
  721. }
  722.  
  723.  
  724.  
  725.  
  726. //-----------------------------------------------------------------------------
  727. // Name: CreateEarthVertexBuffer()
  728. // Desc: Sets up the vertices for a bump-mapped sphere.
  729. //-----------------------------------------------------------------------------
  730. HRESULT CMyD3DApplication::CreateEarthVertexBuffer()
  731. {
  732.     SAFE_RELEASE( m_pEarthVB );
  733.  
  734.     // Choose a tesselation level
  735.     DWORD dwNumSphereRings    = m_bHighTesselation ? 15 :  5;
  736.     DWORD dwNumSphereSegments = m_bHighTesselation ? 30 : 10;
  737.     m_dwNumSphereVertices = 2 * dwNumSphereRings * (dwNumSphereSegments+1);
  738.  
  739.     // Create the vertex buffer
  740.     if( FAILED( m_pd3dDevice->CreateVertexBuffer( m_dwNumSphereVertices*sizeof(BUMPVERTEX),
  741.                                                   D3DUSAGE_WRITEONLY, D3DFVF_BUMPVERTEX,
  742.                                                   D3DPOOL_MANAGED, &m_pEarthVB ) ) )
  743.         return E_FAIL;
  744.  
  745.     return S_OK;
  746. }
  747.  
  748.  
  749.  
  750.  
  751. //-----------------------------------------------------------------------------
  752. // Name: SetMenuStates()
  753. // Desc:
  754. //-----------------------------------------------------------------------------
  755. VOID CMyD3DApplication::SetMenuStates()
  756. {
  757.     HMENU hMenu = GetMenu( m_hWnd );
  758.  
  759.     CheckMenuItem( hMenu, IDM_TEXTURETOGGLE,
  760.                     m_bTextureOn ? MF_CHECKED : MF_UNCHECKED );
  761.     CheckMenuItem( hMenu, IDM_BUMPMAPTOGGLE,
  762.                     m_bBumpMapOn ? MF_CHECKED : MF_UNCHECKED );
  763.     CheckMenuItem( hMenu, IDM_ENVMAPTOGGLE,
  764.                     m_bEnvMapOn ? MF_CHECKED : MF_UNCHECKED );
  765.  
  766.     CheckMenuItem( hMenu, IDM_U8V8L8,
  767.                     m_BumpMapFormat==D3DFMT_X8L8V8U8 ? MF_CHECKED : MF_UNCHECKED );
  768.     CheckMenuItem( hMenu, IDM_U5V5L6,
  769.                     m_BumpMapFormat==D3DFMT_L6V5U5 ? MF_CHECKED : MF_UNCHECKED );
  770.     CheckMenuItem( hMenu, IDM_U8V8,
  771.                     m_BumpMapFormat==D3DFMT_V8U8 ? MF_CHECKED : MF_UNCHECKED );
  772.  
  773.     CheckMenuItem( hMenu, IDM_LOW_TESSELATION,
  774.                     (!m_bHighTesselation) ? MF_CHECKED : MF_UNCHECKED );
  775.     CheckMenuItem( hMenu, IDM_HIGH_TESSELATION,
  776.                     m_bHighTesselation ? MF_CHECKED : MF_UNCHECKED );
  777. }
  778.  
  779.  
  780.  
  781.  
  782. //-----------------------------------------------------------------------------
  783. // Name: MsgProc()
  784. // Desc: Message proc function to handle key and menu input
  785. //-----------------------------------------------------------------------------
  786. LRESULT CMyD3DApplication::MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam,
  787.                                     LPARAM lParam )
  788. {
  789.     // Pass mouse messages to the ArcBall so it can build internal matrices
  790.     m_ArcBall.HandleMouseMessages( hWnd, uMsg, wParam, lParam );
  791.  
  792.     // Trap context menu
  793.     if( WM_CONTEXTMENU == uMsg )
  794.         return 0;
  795.  
  796.     // Handle menu commands
  797.     if( WM_COMMAND == uMsg )
  798.     {
  799.         switch( LOWORD(wParam) )
  800.         {
  801.             case IDM_TEXTURETOGGLE:
  802.                 m_bTextureOn = !m_bTextureOn;
  803.                 break;
  804.  
  805.             case IDM_BUMPMAPTOGGLE:
  806.                 m_bBumpMapOn = !m_bBumpMapOn;
  807.                 break;
  808.  
  809.             case IDM_ENVMAPTOGGLE:
  810.                 m_bEnvMapOn  = !m_bEnvMapOn;
  811.                 break;
  812.  
  813.             case IDM_U8V8L8:
  814.                 SAFE_RELEASE( m_psBumpMap );
  815.                 m_BumpMapFormat = D3DFMT_X8L8V8U8;
  816.                 InitBumpMap();
  817.                 break;
  818.  
  819.             case IDM_U5V5L6:
  820.                 SAFE_RELEASE( m_psBumpMap );
  821.                 m_BumpMapFormat = D3DFMT_L6V5U5;
  822.                 InitBumpMap();
  823.                 break;
  824.  
  825.             case IDM_U8V8:
  826.                 SAFE_RELEASE( m_psBumpMap );
  827.                 m_BumpMapFormat = D3DFMT_V8U8;
  828.                 InitBumpMap();
  829.                 break;
  830.  
  831.             case IDM_LOW_TESSELATION:
  832.                 m_bHighTesselation = FALSE;
  833.                 CreateEarthVertexBuffer();
  834.                 break;
  835.  
  836.             case IDM_HIGH_TESSELATION:
  837.                 m_bHighTesselation = TRUE;
  838.                 CreateEarthVertexBuffer();
  839.                 break;
  840.         }
  841.  
  842.         // Update the menus, in case any state changes occurred
  843.         SetMenuStates();
  844.     }
  845.  
  846.     // Pass remaining messages to default handler
  847.     return CD3DApplication::MsgProc( hWnd, uMsg, wParam, lParam );
  848. }
  849.  
  850.  
  851.  
  852.